﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Domain.ViewModel;

namespace YunSpace.Web.Areas.WorkFlowSetting.Controllers
{
    public class UserButtonController : ControllerBase
    {
        private UserApp userApp = new UserApp();

        [HttpGet]
        public ActionResult SelectUser()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetSelectUserTreeJson()
        {
            // OrganizeUserActionModel
            var organizeandusersdata = userApp.GetOrganizeAndUsers();
            var treeList = new List<TreeViewModel>();
            foreach (OrganizeUserActionModel item in organizeandusersdata)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = item.Children.Any();
                bool hasChildrenUser = item.Users.Any();
                tree.id = item.Organize.F_Id;
                tree.text = item.Organize.F_FullName;
                tree.value = item.Organize.F_Id;
                tree.parentId = item.Organize.F_ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = true;
                
                treeList.Add(tree);

                //if (hasChildrenUser)
                //{
                //    foreach (var user in item.Users)
                //    {
                //        TreeViewModel tree1 = new TreeViewModel();
                //        tree1.id = user.F_Id;
                //        tree1.text = user.F_RealName;
                //        tree1.value = user.F_Id;
                //        tree1.parentId = item.Organize.F_Id;
                //        tree1.isexpand = true;
                //        tree1.complete = true;
                //        tree1.hasChildren = false;
                //        tree1.showcheck = true;
                //        treeList.Add(tree1);
                //    }
                //}

                if (hasChildren)
                {
                    foreach (OrganizeUserActionModel item2 in item.Children)
                    {
                        TreeViewModel tree2 = new TreeViewModel();
                        bool hasChildrenUser2 = item2.Users.Any();
                        tree2.id = item2.Organize.F_Id;
                        tree2.text = item2.Organize.F_FullName;
                        tree2.value = item2.Organize.F_Id;
                        tree2.parentId = item2.Organize.F_ParentId;
                        tree2.isexpand = true;
                        tree2.complete = true;
                        tree2.hasChildren = true;
                        treeList.Add(tree2);

                        if (hasChildrenUser2)
                        {
                            foreach (var user in item2.Users)
                            {
                                TreeViewModel tree1 = new TreeViewModel();
                                tree1.id = user.F_Id;
                                tree1.text = user.F_RealName;
                                tree1.value = user.F_Id;
                                tree1.parentId = item2.Organize.F_Id;
                                tree1.isexpand = true;
                                tree1.complete = true;
                                tree1.hasChildren = false;
                                tree1.showcheck = true;
                                treeList.Add(tree1);
                            }
                        }
                    }
                }
                
            }
            return Content(treeList.TreeViewJson());
        }
        
    }
}
